package com.edu.hre.struts.action.login;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.edu.hre.common.encrypt.Encrypter;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.login.ChangePasswordForm;

public class ChangePasswordAction extends Action {

	private static final Logger logger = Logger.getLogger(ChangePasswordAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "changePassword";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		ChangePasswordForm beanForm = (ChangePasswordForm) form;
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){

			HreUser tmpUser = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
			if (tmpUser != null) {
				beanForm.setUsername(tmpUser.getUserName());
				beanForm.setMessage(StringConstants.BLANK);
			} else {
				beanForm.setMessage("We are really sorry! This function is not available now!");
			}
			return mapping.findForward(ACTION_NAME);
		}

		String oldPassword = CommonUtils.normalizeString(beanForm.getCurrentPassword());
		HreUser tmpUser = null;

		try {
			tmpUser = baseService.getFirstPojoAnd(new String[]{"userName", "userPassword"},
					new int[]{Compare.EQUAL, Compare.EQUAL}, 
					new Object[]{beanForm.getUsername(), Encrypter.encodeMD5(oldPassword)}, null, null, 0, HreUser.class);
		} catch (Exception e) {
			beanForm.setMessage("Some errors occured when the system tried to execute authentication! Please try again later!");
			return mapping.findForward(ACTION_NAME);
		}

		if (tmpUser == null) {
			beanForm.setMessage("Current password is not correct!");
			return mapping.findForward(ACTION_NAME);
		}

		String newPassword = beanForm.getNewPassword();
		String confirmPassword = beanForm.getConfirmPassword();

		String verifyPass = CommonUtils.verifyPassword(newPassword);
		if (!StringConstants.BLANK.equals(verifyPass)) {
			beanForm.setMessage(verifyPass);
			return mapping.findForward(ACTION_NAME);
		}

		if (confirmPassword == null || !confirmPassword.equals(newPassword)) {
			beanForm.setMessage("Confirm password does not match!");
			return mapping.findForward(ACTION_NAME);
		}

		tmpUser.setUserPassword(Encrypter.encodeMD5(newPassword));
		tmpUser.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmpUser);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
}